package com.wksc.framework.impexp.pattern;

import com.wksc.framework.impexp.bean.BaseFormatParams;
import com.wksc.framework.impexp.log.ImpExpLog;
import com.wksc.framework.impexp.util.ClassFactory;

import java.util.Map;

/**
 * <p> Title(文件名): ValStrPattern.java </p>
 * <p> Description(描述): 需要进行进行数值转换的基类 </p>
 * <p> Copyright(版权): Copyright (c) 2011 </p>
 * <p> Company(公司): 成都四方信息技术有限公司 </p>
 * <p> CreateTime(生成日期)：2011-11-23 下午04:12:47 </p>
 * @author wanglei
 * @version impExp
 */
public class  ValStrPattern implements FormatPattern{
	private static final long serialVersionUID = 6338080882339051862L;
	private static final ImpExpLog log = ClassFactory.getLog(ValStrPattern.class);
	protected  Map<Integer, String> map;

    public ValStrPattern(){}

    public ValStrPattern( Map<Integer, String> map) {
        this.map = map;
    }

    @Override
    public synchronized String format(BaseFormatParams params) {
    	//取得待格式化的数据内容
    	String content = String.valueOf(params.getCellContent()).trim();
    	
    	//如果参数内容为空或数值转换表为空直接返回空字符串
    	if(!"".equals(content) && null != map ){
    		//如果数值转换表中包含待转换的内容则进行格式化
    		try{
    			int key = Integer.valueOf(content);
    			if(map.containsKey(key)){
    				return map.get(key);
    			}
    		}catch(NumberFormatException e){
				StringBuffer logInfo = new StringBuffer().append("The value [")
						.append(content).append("] can not be converted!");
				log.operation(logInfo.toString());
				return content;
    		}
    	}
    	return content;
    }
}
